//
//  JD_CommonRequest.m
//  Jared_AFN_Cache
//
//  Created by Jared on 2017/1/22.
//  Copyright © 2017年 Jared. All rights reserved.
//

#import "JD_CommonRequest.h"
#import "InterfaceResultParser.h"

@implementation JD_CommonRequest

- (void)requestListDataWithURL:(NSString *)url params:(NSDictionary *)params requestType:(NetworkRequestType)requestType cacheType:(JDCacheType)cacheType success:(ResponseBodyBlock)success failure:(ResponseBeanBlock)failure{
    JD_AFNetworking *afn = [[JD_AFNetworking alloc] init];
    [afn requestWithURL:url withParams:params withCacheType:cacheType withRequestType:requestType withResult:^(id responseObject, NSError *error) {
        [self handleListDataSuccessWithURL:url responseObject:responseObject success:success failure:failure];
    }];
}

- (void)handleListDataSuccessWithURL:(NSString *)url responseObject:(id)responseObject success:(void(^)(CommonResponseBody *responseBody))success failure:(void(^)(CommonResponseBean *responseBean))failure{
    CommonResponseBody *responseBody = [InterfaceResultParser getResponseBodyWithListDataFromJson:url header:nil resultJson:responseObject];
    if (responseObject) {
        NSString *resultCode = responseBody.responseBean.code;
        if (success) {
            success(responseBody);
        }
//        if ([resultCode isEqualToString:@"200"]) {
//            if (success) {
//                success(responseBody);
//            }
//        }else{
//            if (failure) {
//                failure(responseBody.responseBean);
//            }
//        }
        return;
    }
}

- (void)requestDataWithURL:(NSString *)url params:(NSDictionary *)params requestType:(NetworkRequestType)requestType cacheType:(JDCacheType)cacheType success:(ResponseBodyBlock)success failure:(ResponseBeanBlock)failure{
    JD_AFNetworking *afn = [[JD_AFNetworking alloc] init];
    [afn requestWithURL:url withParams:params withCacheType:cacheType withRequestType:requestType withResult:^(id responseObject, NSError *error) {
        NSLog(@"rsesponse-=-=-=%@", responseObject);
        [self handleDataSuccessWithURL:url responseObject:responseObject success:success failure:failure];
    }];
}

- (void)handleDataSuccessWithURL:(NSString *)url responseObject:(id)responseObject success:(void(^)(CommonResponseBody *responseBody))success failure:(void(^)(CommonResponseBean *responseBean))failure{
    CommonResponseBody *respondBody = [InterfaceResultParser getResponseBodyWithDataFromJson:url header:nil resultJson:responseObject];
    NSLog(@"respondBody===%@", respondBody);
    if (responseObject) {
        NSString *resultCode = respondBody.responseBean.code;
        if (success) {
            success(respondBody);
        }else if (failure){
            failure(respondBody.responseBean);
        }
    }
}

@end
